package com.aaa.base.controller;


import com.aaa.base.service.CommodityService;
import com.aaa.entity.base.Commodity;
import com.aaa.vo.R;
import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author aaa
 * @since 2020-11-23
 */
@RestController
@RequestMapping("/base/commodity")
@Api(value = "")
public class CommodityController {

    @Autowired
    private CommodityService commodityService;

    @PostMapping("findCommodityPage/{page}/{size}")
    @ApiOperation(value = "根据条件分页查询商品")
    public R findCommodityPage(@PathVariable Integer page,@PathVariable Integer size,@RequestBody Commodity commodity){
        Page<Commodity> commodityPage = commodityService.findCommodityPage(page, size, commodity);
        return R.ok().message("查找成功").data("commdity",commodityPage);
    }

    @PostMapping("insertUpdateCommodity")
    @ApiOperation(value = "新增或修改(有Id时是修改，没有Id是增加)")
    public R insertUpdateCommodity(@RequestBody Commodity commodity){
        boolean b = commodityService.saveOrUpdate(commodity);
        if (b){
            return R.ok().message("操作成功");
        }else{
            return R.error().message("操作失败");
        }
    }

    @PostMapping("deleteCommodity/{id}")
    @ApiOperation(value = "根据Id删除")
    public R deleteCommodity(@PathVariable Integer id){
        boolean b = commodityService.removeById(id);
        if (b){
            return R.ok().message("操作成功");
        }else{
            return R.error().message("操作失败");
        }
    }

    @PostMapping("findCommodityExcel/{page}/{size}")
    @ApiOperation(value = "文件导出")
    public R findCommodityExcel(@PathVariable Integer page,@PathVariable Integer size,@RequestBody Commodity commodity){
        try {
            Page<Commodity> p=commodityService.findCommodityPage(page, size, commodity);
            List<Commodity> list=p.getRecords();
            EasyExcel.write("D://商品信息.xls", Commodity.class).sheet("商品信息").doWrite(list);
        }catch (Exception e){
            return R.error().message("导出失败");
        }
        return R.ok().message("导出成功");
    }
    /*通过仓库id查出当前仓库中所有的商品编码和名字*/
    @GetMapping("selectComBarNamByWareId")
    @ApiOperation("通过仓库id查出当前仓库中所有的商品编码和名字")
    public List<Map> selectComBarNamByWareId(){
        Long warehouseid=41L;
         return commodityService.selectComBarNamByWareId(warehouseid);
    }
    /*通过仓库id和商品编码查出该商品的名字和id*/
    @GetMapping("selectComIdNamByWareIdCbar")
    public Commodity selectComIdNamByWareIdCbar(@RequestParam String commoditybar,@RequestParam Long warehouseid){
        QueryWrapper<Commodity> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("bar",commoditybar);
        queryWrapper.eq("warehouseid",warehouseid);
        return commodityService.getOne(queryWrapper);
    }
}

